package com.hankol.message.imConfig

import com.baselibrary.bean.IMUserBean
import com.baselibrary.extension.checkNotNull
import com.baselibrary.utils.GsonUtils
import com.baselibrary.utils.TimeUtil
import com.hankol.message.R
import io.rong.imkit.conversationlist.model.BaseUiConversation
import io.rong.imkit.conversationlist.model.SingleConversation
import io.rong.imkit.conversationlist.provider.BaseConversationProvider
import io.rong.imkit.userinfo.RongUserInfoManager
import io.rong.imkit.widget.adapter.IViewProviderListener
import io.rong.imkit.widget.adapter.ViewHolder
import io.rong.imlib.model.Conversation

//在这里修改 消息列表 ui
class CustomConversationProvider : BaseConversationProvider() {
    override fun isItemViewType(item: BaseUiConversation): Boolean {
        return item.mCore.getConversationType().equals(Conversation.ConversationType.PRIVATE);
    }

    override fun bindViewHolder(holder: ViewHolder, uiConversation: BaseUiConversation, position: Int, list: MutableList<BaseUiConversation>?, listener: IViewProviderListener<BaseUiConversation>?) {
        super.bindViewHolder(holder, uiConversation, position, list, listener)

        val unreadCount = uiConversation.mCore.unreadMessageCount
        if (unreadCount > 99) {
            holder.setImageResource(R.id.rc_conversation_unread_bg, R.drawable.shape_unread_count_bg_large)
        } else {
            holder.setImageResource(R.id.rc_conversation_unread_bg, R.drawable.shape_unread_count_bg_normal)
        }
        holder.setText(R.id.rc_conversation_date, TimeUtil.fromToday(if (uiConversation.mCore.receivedTime!=0L)uiConversation.mCore.sentTime else uiConversation.mCore.receivedTime));
        if (uiConversation is SingleConversation) {
            if (RongUserInfoManager.getInstance().getUserInfo(uiConversation.mCore.targetId).checkNotNull() && RongUserInfoManager.getInstance().getUserInfo(uiConversation.mCore.targetId).extra.checkNotNull()) {
                if (GsonUtils.parseJsonToBean(RongUserInfoManager.getInstance().getUserInfo(uiConversation.mCore.targetId).extra, IMUserBean::class.java).checkNotNull()) {
                    holder.setVisible(R.id.img_vip_tag, GsonUtils.parseJsonToBean(RongUserInfoManager.getInstance().getUserInfo(uiConversation.mCore.targetId).extra, IMUserBean::class.java).isFans == 1)
                }
            }
        }
    }
}